<?php
// 应用公共文件

function is_mobile($number = '')
{
  return preg_match("/^1[3456789]{1}\d{9}$/",$number);
}

//获取指定用户 可提现金额
function getDeposit($uid = '')
{
  $reward = think\facade\Db::name('reward')
                ->where('to_uid',$uid)
                ->where('order_status','>',2)
                ->sum('fee');
  $deposit = think\facade\Db::name('deposit')
                ->where('uid',$uid)
                ->where('status',1)
                ->sum('fee');
  $fee = $reward - $deposit;
  return round($fee,2);
}


//获取指定用户团队的UIDS 包含自己
function getTeamUids($uid = ''){
  $uidss = [$uid];
  $ruids = array();
  for ($i=0; $i < 3; $i++) { 
    
    $uidss = think\facade\Db::name('User')->whereIn('pid',$uidss)->column('id');
      
    if ($uidss) {
      $ruids = array_merge($uidss,$ruids);
      // print_r($ruids);exit();
    }else{
      break;
    }
  }
  return $ruids;
}
//获取指定用户团队的UIDS 不包含自己
function getTeamUidsNoMy($uid = ''){
  $uidss = [$uid];
  $ruids = array();
  for ($i=0; $i < 3; $i++) { 
    
    $uidss = think\facade\Db::name('User')->whereIn('pid',$uidss)->column('id');
      
    if ($uidss) {
      $ruids = array_merge($uidss,$ruids);
      // print_r($ruids);exit();
    }else{
      break;
    }
  }
  return $ruids;
}

//获取指定用户团队 指定级别的UIDS 不包含自己
function getTeamUidsLevel($uid = '',$level = 1){
  $uidss = [$uid];
  $ruids = array();
  for ($i=0; $i < $level; $i++) { 
    
    $uidss = think\facade\Db::name('User')->whereIn('pid',$uidss)->column('id');
      
    // if ($uidss) {
    //   // $ruids = array_merge($uidss,$ruids);
    //   $ruids = array_merge($uidss,$ruids);
    //   // print_r($ruids);exit();
    // }else{
    //   break;
    // }
  }
  return $uidss;
}

function toDatetime( $time, $format = 'Y-m-d H:i:s' ) {
  if ( empty ( $time ) ) {
    return "";
  }
  if ( is_numeric( $time ) ) {
    return date( $format, $time );
  }
  $format = str_replace( '#', ':', $format );
  return date( $format, strtotime( $time ) );
}

function toDate( $time, $format = 'Y-m-d' ) {
  if ( empty ( $time ) ) {
    return $time;
  }
  $ckdate = explode("-", $time);
  if (count($ckdate) > 2) {
    return $time;
  }
  // $is_date=is_int($time);
  // if (!$is_date) return $time;
  $format = str_replace( '#', ':', $format );
  return date( $format, $time );
}

function friendlyDate($sTime,$type = 'normal',$alt = 'false') {
    if (!$sTime)
    return '';
    //sTime=源时间，cTime=当前时间，dTime=时间差
    $cTime      =   time();
    $dTime      =   $cTime - $sTime;
    $dDay       =   intval(date("z",$cTime)) - intval(date("z",$sTime));
    //$dDay     =   intval($dTime/3600/24);
    $dYear      =   intval(date("Y",$cTime)) - intval(date("Y",$sTime));
    //normal：n秒前，n分钟前，n小时前，日期
    if($type=='normal'){
        if( $dTime < 60 ){
            if($dTime < 10){
              return date('H:i:s',$sTime);
                // return '刚刚';    //by yangjs
            }else{
              return date('H:i:s',$sTime);
                // return intval(floor($dTime / 10) * 10)."秒前";
            }
        }elseif( $dTime < 3600 ){
            return date('H:i',$sTime);
            // return intval($dTime/60)."分钟前";
            //今天的数据.年份相同.日期相同.
        }elseif( $dYear==0 && $dDay == 0  ){
            //return intval($dTime/3600)."小时前";
            return date('H:i',$sTime);
            // return '今天'.date('H:i',$sTime);
        }elseif($dYear==0){
            return date("m月d日 H:i",$sTime);
        }else{
            return date("Y-m-d H:i",$sTime);
        }
    }elseif($type=='mohu'){
        if( $dTime < 60 ){
            return $dTime."秒前";
        }elseif( $dTime < 3600 ){
            return intval($dTime/60)."分钟前";
        }elseif( $dTime >= 3600 && $dDay == 0  ){
            return intval($dTime/3600)."小时前";
        }elseif( $dDay > 0 && $dDay<=7 ){
            return intval($dDay)."天前";
        }elseif( $dDay > 7 &&  $dDay <= 30 ){
            return intval($dDay/7) . '周前';
        }elseif( $dDay > 30 ){
            return intval($dDay/30) . '个月前';
        }
        //full: Y-m-d , H:i:s
    }elseif($type=='full'){
        return date("Y-m-d , H:i:s",$sTime);
    }elseif($type=='ymd'){
        return date("Y-m-d",$sTime);
    }else{
        if( $dTime < 60 ){
            return $dTime."秒前";
        }elseif( $dTime < 3600 ){
            return intval($dTime/60)."分钟前";
        }elseif( $dTime >= 3600 && $dDay == 0  ){
            return intval($dTime/3600)."小时前";
        }elseif($dYear==0){
            return date("Y-m-d H:i:s",$sTime);
        }else{
            return date("Y-m-d H:i:s",$sTime);
        }
    }
}


function cut( $Str, $Length,$sss = '' ) {
    //$Str为截取字符串，$Length为需要截取的长度
    global $s;
    $i = 0;
    $l = 0;
    $ll = strlen( $Str );
    $s = $Str;
    $f = true;
    //if(isset($Str{$i}))
    while ( $i <= $ll ) {
    if ( ord( $Str{$i} ) < 0x80 ) {
    $l++; $i++;
    } else if ( ord( $Str{$i} ) < 0xe0 ) {
    $l++; $i += 2;
    } else if ( ord( $Str{$i} ) < 0xf0 ) {
    $l += 2; $i += 3;
    } else if ( ord( $Str{$i} ) < 0xf8 ) {
    $l += 1; $i += 4;
    } else if ( ord( $Str{$i} ) < 0xfc ) {
    $l += 1; $i += 5;
    } else if ( ord( $Str{$i} ) < 0xfe ) {
    $l += 1; $i += 6;
    }

    if ( ( $l >= $Length - 1 ) && $f ) {
    $s = substr( $Str, 0, $i );
    $f = false;
    }

    if ( ( $l > $Length ) && ( $i < $ll ) ) {
    $s = $s . '...'; break; //如果进行了截取，字符串末尾加省略符号“...”
    // $s = $s . $sss; break; //如果进行了截取，字符串末尾加省略符号“...”
    }
    }
  return $s;
}

function strFilter($str){
    $str = str_replace('`', '', $str);
    $str = str_replace('·', '', $str);
    $str = str_replace('~', '', $str);
    $str = str_replace('!', '', $str);
    $str = str_replace('！', '', $str);
    $str = str_replace('@', '', $str);
    $str = str_replace('#', '', $str);
    $str = str_replace('$', '', $str);
    $str = str_replace('￥', '', $str);
    $str = str_replace('%', '', $str);
    $str = str_replace('^', '', $str);
    $str = str_replace('……', '', $str);
    $str = str_replace('&', '', $str);
    $str = str_replace('*', '', $str);
    $str = str_replace('(', '', $str);
    $str = str_replace(')', '', $str);
    $str = str_replace('（', '', $str);
    $str = str_replace('）', '', $str);
    $str = str_replace('-', '', $str);
    $str = str_replace('_', '', $str);
    $str = str_replace('——', '', $str);
    $str = str_replace('+', '', $str);
    $str = str_replace('=', '', $str);
    $str = str_replace('|', '', $str);
    $str = str_replace('\\', '', $str);
    $str = str_replace('[', '', $str);
    $str = str_replace(']', '', $str);
    $str = str_replace('【', '', $str);
    $str = str_replace('】', '', $str);
    $str = str_replace('{', '', $str);
    $str = str_replace('}', '', $str);
    $str = str_replace(';', '', $str);
    $str = str_replace('；', '', $str);
    $str = str_replace(':', '', $str);
    $str = str_replace('：', '', $str);
    $str = str_replace('\'', '', $str);
    $str = str_replace('"', '', $str);
    $str = str_replace('“', '', $str);
    $str = str_replace('”', '', $str);
    $str = str_replace(',', '', $str);
    $str = str_replace('，', '', $str);
    $str = str_replace('<', '', $str);
    $str = str_replace('>', '', $str);
    $str = str_replace('《', '', $str);
    $str = str_replace('》', '', $str);
    $str = str_replace('.', '', $str);
    $str = str_replace('。', '', $str);
    $str = str_replace('/', '', $str);
    $str = str_replace('、', '', $str);
    $str = str_replace('?', '', $str);
    $str = str_replace('？', '', $str);
    return trim($str);
}

function checkPwLevel($pwd){
  if (strlen($pwd)>30 || strlen($pwd)<6)
  {
    return "密码必须为6-30位的字符串";
  }
  if(preg_match("/^\d*$/",$pwd))
  {
    return "密码必须包含字母,强度:弱";//全数字
  }
  if(preg_match("/^[a-z]*$/i",$pwd))
  {
    return "密码必须包含数字,强度:中";//全字母
  }
  if(!preg_match("/^[a-z\d]*$/i",$pwd))
  {
    return "密码只能包含数字和字母,强度:强";//有数字有字母 ";
  }
  return '';
}

function getSettingCode($code = '')
{
  $arr = think\facade\Db::name('setting')
                  ->where('code',$code)
                  ->find();
  return $arr['value'];
}

function getSettingType($type = '')
{
  $list = think\facade\Db::name('setting')
                  ->where('type',$type)
                  ->select()->toArray();
  $arr = array();
  foreach ($list as $k) {
    $arr[$k['field']] = $k['value'];
  }
  return $arr;
}
function getSettingGroup($group = 'base')
{
  $list = think\facade\Db::name('setting')
                  ->where('group',$group)
                  ->select()->toArray();
  $arr = array();
  foreach ($list as $k) {
    $arr[$k['field']] = $k['value'];
  }
  return $arr;
}


function mpcfg(){
  $mini = getSettingType('公众号');
  $config = [
    'app_id' => $mini['mp_app_id'],
    'secret' => $mini['mp_app_secret'],
    'token' => $mini['mp_token'],
    'aes_key' => $mini['mp_aes_key'],
    'response_type' => 'array',
    // 'log' => [
    //   'default' => 'dev', // 默认使用的 channel，生产环境可以改为下面的 prod
    //   'channels' => [
    //       // 测试环境
    //       'dev' => [
    //           'driver' => 'single',
    //           'path' => './easywechat.log',
    //           'level' => 'debug',
    //       ],
    //       // 生产环境
    //       'prod' => [
    //           'driver' => 'daily',
    //           'path' => './easywechat.log',
    //           'level' => 'info',
    //       ],
    //   ],
    // ],
  ];
  return $config;
}

function minicfg(){
  
  $appId = input('appId');
  $appPlatform = input('appPlatform');
  if (!$appId) {
    $v = think\facade\Db::name('setting')
                  ->where('code','mini_app_id')
                  ->where('field','mini_app_id')
                  ->find();
    if ($v) {
      $appId = $v['value'];
    }
  }
  
  $v = think\facade\Db::name('setting')
                  ->where('code','mini_app_secret')
                  ->where('field','mini_app_secret')
                  ->find();
  if ($v) {
    $secret = $v['value'];
  }
  $cfg = [
      'app_id' => $appId,
      'secret' => $secret,
      // 下面为可选项
      'response_type' => 'array',
      // 'log' => [
      //     'level' => 'debug',
      //     'file' => __DIR__.'/wechat.log',
      // ],
    ];
  return $cfg;
}